package com.zhangwei.fresco.ui;

import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zhangwei.fresco.R;
import com.zhangwei.fresco.apdater.VpPageAdapter;
import com.zhangwei.fresco.fragment.CommFragment_;
import com.zhangwei.fresco.utils.ActivitiesManager;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

@EActivity(R.layout.activity_main)
public class MainActivity extends BaseActivity {
    @Extra
    String text;
    @ViewById
    Toolbar toolbar;
    @ViewById
    TabLayout tabs;
    @ViewById
    ViewPager vpMain;
    @ViewById
    SimpleDraweeView simple;
    //记录上一次按下返回键的时间
    private long exitTime;


    @AfterViews
    public void initView() {
        initTabs();
    }

    /**
     * 占位图  默认
     *
     */
    @AfterInject
    public void inject() {
        //4:3
        simple.setAspectRatio(1.33f);
        simple.setImageURI(Uri.parse(""));
    }

    /**
     *
     */
    private void initTabs() {
        tabs.addTab(tabs.newTab().setText("圣诞节"));
        tabs.addTab(tabs.newTab().setText("圣诞节"));
        tabs.addTab(tabs.newTab().setText("圣诞节"));
        tabs.addTab(tabs.newTab().setText("圣诞节"));
        tabs.setupWithViewPager(vpMain);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new CommFragment_());
        fragments.add(new CommFragment_());
        fragments.add(new CommFragment_());
        fragments.add(new CommFragment_());
        tabs.setTabsFromPagerAdapter(new VpPageAdapter(getSupportFragmentManager(), fragments));
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            app.showToast(getString(R.string.exit_str));
            // 计算两次返回键按下的时间差
            exitTime = System.currentTimeMillis();
        } else {
            //注销Activity的监听
            app.unRegisterActivityLifecycleCallbacks();
            //结束应用程序
            ActivitiesManager.getAppManager().AppExit(app);
            // 返回桌面操作
            // Intent home = new Intent(Intent.ACTION_MAIN);
            // home.addCategory(Intent.CATEGORY_HOME);
            // startActivity(home);
        }
    }


    //相当于 线程的run方法
    @Background
    public void doBackground() {

    }

    /**
     * 主线程执行的代码
     */
    @UiThread
    public void uiThread() {

    }


}
